#!/usr/bin/env bash
DIR=$(cd "$(dirname "$0")"; pwd)
pushd $DIR

pushd ..
git pull

#set web site name
if [ ! -n "$1" ]
then
echo 'default site: www'
site='web'
else
echo "deploying site: $1"
site=$1
fi

#set web site port
if [ ! -n "$2" ]
then
echo 'default port: ""'
tomcat_port=''
else
echo "port is $2"
tomcat_port=$2
fi

#set web site env
if [ ! -n "$3" ]
then
echo 'default env: ""'
env=''
else
echo "env is $3"
env=$3
fi


#set project name which is going to be deployed
project_name=`echo $site| sed 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/'`
#if [ $project_name = "skx-visi" ]
#then
#project_name=$project_name"-web"
#fi
echo "deployed web site is " $project_name

#set tomcat home
tomcat_home=$(awk -F'=' '/tomcat_home_on_linux/{print $2}' ./gradle.properties|tr  '\r' ' '|awk '{print $1}')
tomcat_home="$tomcat_home$tomcat_port"
echo $tomcat_home
shutdown=$tomcat_home/bin/shutdown.sh
startup=$tomcat_home/bin/startup.sh

#shutdown tomcat
eval $shutdown
sleep 1s

#echo rm ROOT folder and ROOT.war file...
#mv $tomcat_home/webapps/ROOT.war /data/ROOT_War_Backup
#rm -rf $tomcat_home/webapps/ROOT

#excute gradle deploy method
echo  "./gradlew $site" -Ptomcat_port=$tomcat_port -Penv=$env
eval "./gradlew $site" -Ptomcat_port=$tomcat_port -Penv=$env
#scp -P36022 /home/oschina/pandora/pandora-weixin-web/build/libs/pandora-weixin-web-1.0-SNAPSHOT.war root@a.ly.shikongxian.com:/etc/vj/tomcat/webapps/weixin.war
#startup tomcat
eval $startup
popd
popd
